<template>
  <view v-if="shenheStatus != 1 || !shenheAliStatus" class="page">
    <view class="content" v-if="form">
      <view class="title">{{ form.name }}</view>

      <toolsForm :form.sync="form" :disabled="disabled" ref="form"></toolsForm>

      <view
        class="submit safe-area-inset-bottom"
        @click="submit"
        v-if="!disabled"
      >
        <view class="m-btn">确认提交 </view>
      </view>
    </view>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from "@/components/shenhe/shenhe";
import toolsForm from "./components/tools-form.vue";

export default {
  components: {
    Shenhe,
    toolsForm
  },
  data() {
    return {
      goods_id: "",
      form: null,
      disabled: false
    };
  },
  async onLoad(opi) {
    if (opi.disabled) {
      const res = await this.$allrequest.goods.formDataInfo({
        id: opi.id
      });
      this.form = res.data.form;
      this.disabled = true;
      return;
    }

    if (opi.goods_id) this.goods_id = opi.goods_id;
    const res = await this.$allrequest.goods.formInfo({
      goods_id: this.goods_id
    });
    if (!res.code) {
      this.form = res.data;

      if (!this.form.list.length) {
        this.$utils.showModal(
          "提示",
          "当前商品未开启表单提交功能",
          false,
          () => {
            uni.navigateBack();
          },
          () => {}
        );
      }

      if (!this.form.is_form) {
        this.disabled = true;
      }
    }
  },
  methods: {
    submit() {
      this.$u.throttle(async () => {
        try {
          this.form.list.forEach((item) => {
            if (item.required == 1 && item.default.length == 0) {
              throw item.tip;
            }
            if (item.type == "checkbox" && Array.isArray(item.default)) {
              item.default = item.default.join(",");
            }
          });

          const res = await this.$allrequest.goods.formDataSave(
            {
              goods_id: this.goods_id,
              form: JSON.stringify(this.form)
            },
            true
          );

          if (!res.code) {
            this.$utils.showModal("提示", "提交成功", false, () => {
              this.$utils.toUrl("/showGoodsForm/result/index", "redirectTo");
            });
            return;
          }
        } catch (error) {
          this.$utils.toast(error || "请完善表单必填项");
        }
      }, 500);
    }
  }
};
</script>

<style lang="scss" scoped>
.page {
  background: #f2f2f2;
  padding: 24rpx;
  min-height: 100vh;
}

.content {
  margin-bottom: 180rpx;
  padding: 0 24rpx 24rpx;
  border-radius: 10px;
  background: #fff;

  .title {
    padding: 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    border-bottom: 1px solid #efefef;
  }
}

.submit {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;

  .m-btn {
    margin: 10rpx 36rpx;
    height: 80rpx;
    background: #f0250e;
    border-radius: 32px;
    text-align: center;
    color: #fff;
    font-size: 32rpx;
    line-height: 80rpx;
  }
}
</style>
